package com.xiaoj;

/*
 * 测试 join 的使用
 */
public class ThreadDemo7 {

    public static void main5(String[] args) throws InterruptedException {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("线程名 = " + Thread.currentThread().getName() + "，程序员小杰已经跑步5公里了");
            }
        });
        t1.start();
        t1.join();
        // 这行代码必须要等t1全部执行完毕，才会执行
        System.out.println("线程名 = " + Thread.currentThread().getName() + "，跑完了吧，回家吧！");
    }

    public static void main2(String[] args) throws InterruptedException {
        Thread t1 = new Thread(new Runnable() {
            private Integer count = 0;

            @Override
            public void run() {
                for (int i = 0; i < 2; i++) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("线程名 = " + Thread.currentThread().getName() + "，程序员小杰已经跑步" + (++count) + "公里了");

                }
            }
        });
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 3; i++) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("线程名 = " + Thread.currentThread().getName() + "程序员小静在买吃的");
                }
            }
        });

        t1.start();
        t2.start();
        t1.join();
        // 这行代码必须要等t1全部执行完毕，才会执行
        System.out.println("线程名 = " + Thread.currentThread().getName() + "，跑完了吧，回家吧！");
    }

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(new Runnable() {
            private Integer count = 0;

            @Override
            public void run() {
                for (int i = 0; i < 2; i++) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("线程名 = " + Thread.currentThread().getName() + "，程序员小杰已经跑步" + (++count) + "公里了");

                }
            }
        });
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //调用 t1 的 join 方法
                    t1.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                for (int i = 0; i < 3; i++) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("线程名 = " + Thread.currentThread().getName() + "程序员小静在买吃的");
                }
            }
        });

        t1.start();
        t2.start();
        //调用 t2 的 join 方法
        t2.join();
        // 这行代码必须要等t1全部执行完毕，才会执行
        System.out.println("线程名 = " + Thread.currentThread().getName() + "，跑完了吧，回家吧！");
    }
}